package com.power.sifa.correction.service.impl;

import com.plf.core.common.service.BaseServiceImpl;
import com.power.sifa.correction.dao.Crw_rule_disabledDao;
import com.power.sifa.correction.model.Crw_rule_disabled;
import com.power.sifa.correction.model.pageInfo.Crw_rule_disabledPageInfo;
import com.power.sifa.correction.service.Crw_rule_disabledService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 *  人员-屏蔽规则业务层实现
 */
@Service
public class Crw_rule_disabledServiceImpl extends BaseServiceImpl implements Crw_rule_disabledService {

    @Autowired
    private Crw_rule_disabledDao dao;
    @Resource
    private RuleRecordCacheService ruleRecordCacheService;

    @Override
    public Crw_rule_disabled getById(Serializable id) {
        return dao.findById(id).orElse(null);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveOrUpdate(Crw_rule_disabled crw_rule_disabled) {
    	// save 如果数据库不存在这条数据，就会执行保存（即将这条数据插入到数据库）；如果数据库存在这条数据，就会根据主键去更新这条数据
        if (crw_rule_disabled.getPrimaryKey() != null) {
        	 Crw_rule_disabled crw_rule_disabledfDb = dao.findById(crw_rule_disabled.getPrimaryKey()).orElse(null);
             if(crw_rule_disabledfDb != null) {
                BeanUtils.copyProperties(crw_rule_disabled, crw_rule_disabledfDb);
                dao.save(crw_rule_disabledfDb);
                return;
             }
        }
        dao.save(crw_rule_disabled);
    }

    @Override
    public void removeById(String id) {
        List<Crw_rule_disabled> disableds = dao.findByIds(Arrays.asList(id));
        dao.deleteById(id);
    }

    @Override
    public Page<Crw_rule_disabled> findAll(Crw_rule_disabledPageInfo pageInfo) {
        //排序等定义
        Sort sort;
        if("asc".equals(pageInfo.getOrder())) {
            sort = Sort.by(Sort.Direction.ASC, pageInfo.getSort());
        }else {
            sort = Sort.by(Sort.Direction.DESC, pageInfo.getSort());
        }
        //分页的定义
        Pageable pageable = PageRequest.of(pageInfo.getPageNumber()<=1 ? 0 : pageInfo.getPageNumber()-1, pageInfo.getPageSize(), sort);
        //查询条件
        Specification spec = this.createSpecification(pageInfo);
        return dao.findAll(spec, pageable);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeByIds(List<String> ids) {
        List<Crw_rule_disabled> disableds = dao.findByIds(ids);
        dao.removeByIds(ids);
    }
    
    @Override
	public List<Crw_rule_disabled> findAll() {
		return dao.findAll();
	}
	
	@Override
	public void saveAll(List<Crw_rule_disabled> list) {
		dao.saveAll(list);
	}

    @Override
    public List<Crw_rule_disabled> findDisabledRuleByPersonId(String personId) {
        return dao.findDisabledRuleByPersonId(personId);
    }


    /**
     * service 层进行新增，删除。修改后同步到redis数据 TOFIX: 暂时不使用该方法，避免redis和数据库查询不一致
     * @param personIds
     */
    private void setRedisCacheByPersons(List<String> personIds) {
        CompletableFuture.runAsync(() ->
                personIds.stream().forEach(el -> {
                    ruleRecordCacheService.initRuleCacheByPersonId(el);
                })
        );
    }
}
